//
//  SinaStatusListViewModel.m
//  oc微博
//
//  Created by 安陆与 on 16/1/20.
//  Copyright © 2016年 安陆与. All rights reserved.
//

#import "SinaStatusListViewModel.h"
#import "AFNetworking.h"
#import "SinaAccountViewModel.h"
#import "MJExtension.h"
#import "SinaStatusModel.h"
#import "user.h"
#import "SinaStatusViewModel.h"
@interface SinaStatusListViewModel()


@end

@implementation SinaStatusListViewModel





-(void)getData:(void(^)())success failure:(void(^)())failure{

    //创建一个manger
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
    //拼接参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"access_token"] = [[SinaAccountViewModel alloc]init].access_token;
    //发送一个get请求
    
    NSLog(@"%@",params);
    //增加解析类型
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    
    [manger GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:params  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        NSLog(@"%@",responseObject);
        if (responseObject == nil) {
         
            return ;
        }
        
        NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:20];
        
        SinaStatusModel *model = [[SinaStatusModel alloc]init];
        
        for (NSDictionary *dict in responseObject[@"statuses"]) {
            model = [SinaStatusModel mj_objectWithKeyValues:dict];
            
            SinaStatusViewModel * viewModel = [[SinaStatusViewModel alloc]init:model];
            
            
            [tempArray addObject:viewModel];
        }
        
       _list = tempArray.copy;
        
        success();
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"-----%@",error);
    }];

}

@end
